#!/bin/bash
#
# $Id$
#

gfxcard=$(lspci |grep VGA |awk -F: {' print $3 '} |awk {'print $1'})

other () {
# Update the os and install dependencies
sudo apt-get update
sudo apt-get -y dist-upgrade
sudo apt-get -y install ssh python-software-properties vim udisks upower xorg alsa-utils mesa-utils avahi-daemon


# add curent user to some groups
sudo adduser `id -un` video
sudo adduser `id -un` audio

# Install XBMC PVR Branch
sudo apt-get -y install curl
curl -Ls --output /tmp/install-xbmc.sh http://packages.pulse-eight.net/ubuntu/install-xbmc.sh 
bash /tmp/install-xbmc.sh

# Install autostart on boot scripts and configs
sudo sed -i 's/allowed_users=.*/allowed_users=anybody/g' /etc/X11/Xwrapper.config
sudo wget http://pastebin.com/raw.php?i=z2Ya7sAh -O /etc/init/xbmc.conf
sudo ln -s /lib/init/upstart-job /etc/init.d/xbmc
sudo wget http://paste.kde.org/7022/raw/ -O /usr/bin/runXBMC
sudo chmod a+x /usr/bin/runXBMC

# Install Lirc
sudo apt-get -y install lirc
sudo wget http://pastebin.com/raw.php?i=A7aCKkMa -O /etc/udev/rules.d/90-enable-remote-wakeup.rules
sudo wget http://paste.kde.org/200066/raw/ -O /etc/polkit-1/localauthority/50-local.d/xbmc_rule.pkla


# System Tunning...
sudo /etc/init.d/apparmor stop
sudo /etc/init.d/apparmor teardown
sudo apt-get -y remove apparmor
sudo bash -c "echo dev.cdrom.lock=0 >>/etc/sysctl.conf"
sudo bash -c "echo vm.swappiness=10 >>/etc/sysctl.conf"


# if Sundtek USB Stick is found install drivers and tvheadend
if [ $sundtek ];then
        cd /tmp
        wget http://www.sundtek.de/media/sundtek_installer_development.sh
        sudo sh ./sundtek_installer_development.sh -easyvdr -nolirc
        sudo apt-get -y install tvheadend

	cat << EOF | sudo tee /etc/sundtek.conf >/dev/null
loglevel=off
use_hwpidfilter=on
ir_disabled=1
bulk_notification=on
device_attach=/opt/bin/attach.sh
initial_dvb_mode=DVBC
disable_atv=1

EOF

	cat << EOF | sudo tee /opt/bin/attach.sh >/dev/null
#!/bin/bash

/etc/init.d/tvheadend restart

EOF
sudo chmod +x /opt/bin/attach.sh

	sudo update-rc.d tvheadend disable

fi

# Configure GFX
if [[ $gfxcard == NVIDIA ]];then
        sudo apt-get -y install nvidia-current
	sudo sed -i '/gfxmode=/ a\  set gfxpayload=keep' /etc/grub.d/00_header
	#sudo sed -i 's/#GRUB_GFXMODE=.*/GRUB_GFXMODE=1366x768/g' /etc/default/grub
	sudo sed -i 's/#GRUB_GFXMODE=.*/GRUB_GFXMODE=1920x1080/g' /etc/default/grub
elif [[ $gfxcard == ATI ]];then
        sudo apt-get -y install xvba-va-driver vainfo
elif [[ $gfxcard == AMD ]];then
        sudo apt-get -y install xvba-va-driver vainfo
elif [[ $gfxcard == INTEL ]];then
        sudo apt-get -y install i965-va-driver vainfo
else
	echo "Unknown GFX Card $gfxcard cant install GFX..."
fi

# Fix Boot Logo
sudo apt-get -y install plymouth-label
wget http://excyle.nl/plymouth-theme-xbmc-logo.deb
sudo dpkg -i plymouth-theme-xbmc-logo.deb
sudo sh -c "echo "FRAMEBUFFER=y" > /etc/initramfs-tools/conf.d/splash"

sudo update-grub
sudo update-initramfs -u


# Tunne XBMC Installation
[ -d ~/.xbmc/userdata/keymaps ] || mkdir -p ~/.xbmc/userdata/keymaps

cat << EOF | tee ~/.xbmc/userdata/advancedsettings.xml >/dev/null
<advancedsettings>
 <gui>    
   <algorithmdirtyregions>3</algorithmdirtyregions>
   <nofliptimeout>1000</nofliptimeout>
 </gui>
 <gputempcommand>echo "\$(nvidia-settings -c :0 -tq GPUCoreTemp) C"</gputempcommand>
 <cputempcommand>sensors|sed -ne "s/Core 1: \+[-+]\([0-9]\+\).*/\1 C/p"</cputempcommand> 
 <network>
  <disableipv6>true</disableipv6>
 </network>
 <videolibrary>
    <cleanonupdate>true</cleanonupdate>  <!-- default set to false to prevent xbmc from removing items from the database while updating -->
    <exportautothumbs>true</exportautothumbs>  <!-- export auto-generated thumbs. Defaults to false -->
 </videolibrary>
 <video>
   <cleanstrings append="yes">
      <regexp>[\._ \-][Ss]([0-9]+)</regexp>
   </cleanstrings>
 </video>
</advancedsettings>

EOF

wget -O ~/.xbmc/userdata/keymaps/remote.xml http://calsifer.ath.cx/files/jenkins_remote.xml

cat << EOF | sudo tee /etc/motd.tail >/dev/null
       ___.
___  __\\_ |__   _____   ____
\\  \\/  /| __ \\ /     \\_/ ___\\
 >    < | \\_\\ \\  Y Y  \\  \\___
/__/\\_ \\|___  /__|_|  /\\___  >
      \\/    \\/      \\/     \\/
             http://xbmc.org/

EOF
}

lircfix () {

echo "Adding ~/bin/resetXBMC.sh"
[ -d ~/bin ] || mkdir ~/bin
cat << EOF | tee ~/bin/resetXBMC.sh >/dev/null
#!/bin/bash

sudo stop xbmc
sleep 2
sudo start xbmc
EOF
chmod +x ~/bin/resetXBMC.sh

echo "Adding XBMC sudo rules to /etc/sudoers.d/10_xbmc"
sudo grep -q includedir /etc/sudoers
[ $? -gt 0 ] && sudo sh -c "echo \#includedir /etc/sudoers.d >> /etc/sudoers"
[ -d /etc/sudoers.d ] || mkdir /etc/sudoers.d

cat << EOF | sudo tee /etc/sudoers.d/10_xbmc >/dev/null
Cmnd_Alias SHUTDOWN_CMDS = /sbin/shutdown, /sbin/reboot, /sbin/halt # XBMC
Cmnd_Alias MOUNT_CMDS = /bin/mount, /bin/umount # XBMC
Cmnd_Alias XBMC_CMDS = /sbin/stop xbmc, /sbin/start xbmc # XBMC
Cmnd_Alias SERVICES = /usr/sbin/service

`id -un` ALL=NOPASSWD: SHUTDOWN_CMDS, MOUNT_CMDS, XBMC_CMDS, SERVICES # XBMC

EOF

echo "Adding start of irexec to rc.local"
grep -q irexec /etc/rc.local
[ $? -gt 0 ] && sudo sh -c "echo irexec -d /home/`id -un`/.lircrc >> /etc/rc.local"

echo "Adding irexec conf to  ~/.lircrc"
echo "Adding Remote button Clear as XBMC reset button to  ~/.lircrc"
cat << EOF | tee ~/.lircrc >/dev/null
# resetXBMC
begin
   prog = irexec
   button = Clear
   config = /home/`id -un`/bin/resetXBMC.sh &
end
EOF
sleep 2

echo "Adding /etc/pm/sleep.d/20_irexec to restart irexec on suspend/resume."
cat << EOF | sudo tee /etc/pm/sleep.d/20_irexec >/dev/null
#!/bin/bash
case "\${1}" in
    resume)
                if ps -ef|grep -v grep|grep irexec ;then
                        killall irexec
                fi
                irexec -d /home/`id -un`/.lircrc
        ;;
esac
EOF
sudo chmod +x /etc/pm/sleep.d/20_irexec
}

$1
